package cvo.chess.client.entities;

import cvo.chess.client.entities.armies.BlackArmy;
import cvo.chess.client.entities.armies.Piece;
import cvo.chess.client.entities.armies.WhiteArmy;
import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.game.Army;
import cvo.chess.client.entities.game.Chessboard;
import cvo.chess.client.entities.game.IChessboard;
import cvo.chess.client.entities.game.PositionManager;

/**
 * 
 * @author Stijn.Van.Antwerpen
 *
 * A Game represent a single ChessGame
 * It contains 2 armies and a ChessBoard
 * 
 * The game keeps trace off the positions of the pieces on the board during a game
 */
public class Game {
	
	private final static int BOARD_SIZE = 8;
	
	//for future reference, a game needs an id.
	//for know, this is not used, nor generated
	private Integer id;
	
	private GameFacade gf;
	
	private Army whiteArmy;
	private Army blackArmy;
	private IChessboard board;
	
	private PositionManager positionManager; 
	
	public Game(GameFacade gf){
		
		this.gf = gf;
	}
	
	public void Init(){
		//TODO: generate ID
		this.id = 0;
		
		this.blackArmy = new BlackArmy(gf);
		this.whiteArmy = new WhiteArmy(gf);
		this.board = new Chessboard(BOARD_SIZE);
		this.positionManager = new PositionManager();		
	}
	
	
	public void Start() throws Exception{
		this.whiteArmy.Init();
		this.blackArmy.Init();
	}
	
	public IChessboard getBoard() {
		return board;
	}

	public Piece findPieceAt(ChessboardCoordinate boardCoordinate) {		
		return this.positionManager.pieceAtPosition(boardCoordinate);
	}


	public void setPiece(Piece piece, ChessboardCoordinate position) throws Exception {
		this.positionManager.putPiece(piece, position);
	}

	public void doMove(ChessboardCoordinate fromBc, ChessboardCoordinate toBc) throws Exception {
		//TODO: consider a Move class 
		//something like Move move = new Move(fromBc, toBc);
		//or even Move move = new Move (algebraicNotation);
		
		if(validateMove(fromBc, toBc)){
			Piece piece = this.positionManager.pieceAtPosition(fromBc);
			this.positionManager.movePieceTo(piece, toBc);
		}
		
	}

	private boolean validateMove(ChessboardCoordinate fromBc,
			ChessboardCoordinate toBc) {
		// TODO: Validate the move, is it a legal move?
		return true;
	}

	
	
	
}
